// pages/myTicket/myTicket.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let wxbarcode = require("../../../utils/wxbarcode/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    qrType: false
  },
  getData() {
    const that = this;
    //根据id领取优惠券
    let data = {
      page: that.data.page,
      limit: that.data.pageSize
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.myGiftCoupon, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.washlist.data.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          res.data.result.washlist.data.forEach(item => {
            if (item.status == 1) {
              item.statusMsg = '已使用'
            } else if (item.status == 10) {
              item.statusMsg = '未到使用时间'
            } else if (item.status == 11) {
              item.statusMsg = '已过期'
            }
            if (item.type == 0) {
              item.typeMsg = '钻石'
            } else if (item.type == 1) {
              item.typeMsg = '至尊'
            } else if (item.type == 3) {
              item.typeMsg = '五粮液'
            } else if (item.type == 5) {
              item.typeMsg = '生日'
            } else {
              item.typeMsg = '礼品'
            }
          })
          that.setData({
            isWithout: true,
            listData: res.data.result.washlist.data
          })
        }
      }
    })
  },



  //显示二维码
  showQr(e) {
    const that = this;
    that.setData({
      qrcode: e.currentTarget.dataset.hx_code + '|' + e.currentTarget.dataset.encry,
      qrType: true
    })
    setTimeout(() => {
      that.qrCode( that.data.qrcode)
    }, 200)
  },
  remQr() {
    const that = this;
    that.setData({
      qrType: false
    })
  },
  qrCode(code) {
    const query = this.createSelectorQuery()
    query.select('#qrcode').fields({
      node: true,
      size: true
    }).exec(res => {
      const canvas = res[0].node
      wxbarcode.wxqrcode({
        canvas: canvas,
        canvasId: 'qrcode',
        width: 200,
        padding: 50,
        paddingColor: '#fff',
        background: '#fff',
        foreground: '#000000',
        text: code,
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.getData();
  },
  onShow: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.getData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      page: that.data.page + 1,
      limit: that.data.pageSize
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.myGiftCoupon, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.washlist.data.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          res.data.result.washlist.data.forEach(item => {
            if (item.status == 1) {
              item.statusMsg = '已使用'
            } else if (item.status == 10) {
              item.statusMsg = '未到使用时间'
            } else if (item.status == 11) {
              item.statusMsg = '已过期'
            }
            if (item.type == 0) {
              item.typeMsg = '钻石'
            } else if (item.type == 1) {
              item.typeMsg = '至尊'
            } else if (item.type == 3) {
              item.typeMsg = '五粮液'
            } else if (item.type == 5) {
              item.typeMsg = '生日'
            } else {
              item.typeMsg = '礼品'
            }
          })
          that.setData({
            listData: [...that.data.listData, ...res.data.result.washlist.data],
            page: that.data.page + 1
          })
        }
      }
    })
  },
})